//
//  APILogOn.m
//  Bracelet
//
//  Created by jing zhao on 2/16/15.
//  Copyright (c) 2015 jing zhao. All rights reserved.
//

#import "APILogOn.h"

@implementation APILogOnDeparment

-(instancetype)initDeparmentWithArray:(NSArray*)array
{
    self = [super init];
    if (self) {
        _arrayDepartementId = [[NSMutableArray alloc]init];
        _arrayDepartmentName = [[NSMutableArray alloc]init];
        for (NSString* strTemp in array) {
            NSArray *arrayTmp =[strTemp componentsSeparatedByString:@"@"];
            NSString *strTempId = [arrayTmp objectAtIndex:0];
            NSString *strTempName = [arrayTmp objectAtIndex:1];
            [_arrayDepartementId addObject:strTempId];
            [_arrayDepartmentName addObject:strTempName];
        }
    }
    return self;
}

+(instancetype)apiDeparmentWithArray:(NSArray*)array
{
    return [[self alloc]initDeparmentWithArray:array];
}


@end

@implementation APILogOnCity

-(instancetype)initCityWithArray:(NSArray*)array
{
    self = [super init];
    if (self) {
        
        _arrayCityId = [[NSMutableArray alloc]init];
        _arrayCityChineseName = [[NSMutableArray alloc]init];
        _arrayCityEnglishName = [[NSMutableArray alloc]init];
        for (NSString* strTemp in array) {
            NSArray *arrayTmp =[strTemp componentsSeparatedByString:@"@"];
            NSString *strTempId  = [arrayTmp objectAtIndex:0];
            NSString *strTempEnglishName  = [arrayTmp objectAtIndex:1];
            NSString *strTempChineseName  = [arrayTmp objectAtIndex:2];
            [_arrayCityId addObject:strTempId];
            [_arrayCityEnglishName addObject:strTempEnglishName];
            [_arrayCityChineseName addObject:strTempChineseName];
        }
    }
    return self;
}

+(instancetype)apiCityWithArray:(NSArray*)array
{
    return [[self alloc]initCityWithArray:array];
}
@end

@implementation APILogOn

+(void)setApiLogOnWithUserName:(NSString*)userNameTemp
                 andWithCityId:(NSString*)cityIdTemp
                   andBranchId:(NSString*)branchIdTemp
               andDepartmentId:(NSString*)departmentIdTemp
                   andMobileId:(NSString*)mobileIdTemp
                       success:(void (^)(APILogOn *logOn)) successBlock
                       failure:(void (^)( NSError *error)) failureBlock
{
    NSDictionary *data = @{@"user.userName":userNameTemp,@"user.cityId":cityIdTemp,@"user.branchId":branchIdTemp,@"user.departmentId":departmentIdTemp};
    NSString *strUserId = [[NSUserDefaults standardUserDefaults] objectForKey:USERID];
    NSDictionary *headerData =@{@"BasicAuthUsername":strUserId};
    
    [[APIConnection shareInstance]post:@"workout/saveUserInfo" data:data header:headerData netError:^(NSError *error) {
         failureBlock(error);
    } apiFinish:^(id data) {
        
        NSDictionary *dictionaryTemp = data;
        NSInteger codeTemp= [[dictionaryTemp objectForKey:APIKEYCODE]integerValue];
        if (codeTemp ==0) {
            NSDictionary *dicSuccess = [data objectForKey:APIKEYData];
            NSDictionary* dicData = [dicSuccess objectForKey:APIKEYTUSER];
            NSString *strUserId = [dicData objectForKey:APIKEYUSERID];
            [[NSUserDefaults standardUserDefaults]setObject:strUserId forKey:USERID];
            [[NSUserDefaults standardUserDefaults] setObject:dicData forKey:APIKEYUSERINFO];
            successBlock(nil);
        }
        else
        {
            failureBlock(nil);
        }
    }];
}

+(void)getTheCitywithSuccess:(void (^)(APILogOnCity *logOncity)) successBlock
                     failure:(void (^)( NSError *error)) failureBlock
{
  [[APIConnection shareInstance]post:@"baseInfo/getAllCityInfo" data:nil netError:^(NSError *error) {
       failureBlock(nil);
      
  } apiFinish:^(id data) {
      NSDictionary *dictionaryTemp = data;
      NSInteger codeTemp= [[dictionaryTemp objectForKey:APIKEYCODE]integerValue];
      if (codeTemp ==0) {
          NSDictionary *dicSuccess = [data objectForKey:APIKEYData];
          NSArray *array = [dicSuccess objectForKey:APIKEYCITYINFO];
          successBlock([APILogOnCity apiCityWithArray:array]);
      }
      else
      {
          failureBlock(nil);
      }
  }];
}

+(void)getTheDepartmentwithSuccess:(void (^)(APILogOnDeparment *logOnDepartment)) successBlock
                     failure:(void (^)( NSError *error)) failureBlock
{
    [[APIConnection shareInstance]post:@"baseInfo/getAllDepartmentInfo" data:nil netError:^(NSError *error){
        failureBlock(nil);
    } apiFinish:^(id data) {
        NSDictionary *dictionaryTemp = data;
        NSInteger codeTemp= [[dictionaryTemp objectForKey:APIKEYCODE]integerValue];
        if (codeTemp ==0) {
            NSDictionary *dicSuccess = [data objectForKey:APIKEYData];
            NSArray *array = [dicSuccess objectForKey:APIKEYDEPARTINFO];
            successBlock([APILogOnDeparment apiDeparmentWithArray:array]);
        }
        else
        {
            failureBlock(nil);
        }
    }];
    
}
@end

